package com.ls.controller;

import com.ls.pojo.SysUser;
import com.ls.service.sysUser.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Resource
    private SysUserService sysUserService;

    @RequestMapping(value = "/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping(value = "/sys/toMain")
    public String toFrame() {
        return "frame";
    }

    @RequestMapping(value = "/toSysError")
    public String sysError() {
        return "sysError";
    }

    @RequestMapping(value = "/login")
    public String doLogin(@RequestParam String account, @RequestParam String password,
                          HttpServletRequest request, HttpSession session) throws Exception {
        SysUser sysUser = sysUserService.login(account, password);

        if (sysUser != null) {
            session.setAttribute("userSession", sysUser);
            return "redirect:/sys/toMain";
        } else {
            request.setAttribute("error", "用户名或密码不正确");
            return "login";
        }
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("userSession");
        return "login";
    }
}